package com.itheima.reggie.util;

import com.itheima.reggie.constant.CommonConstant;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.stereotype.Component;

import javax.servlet.http.HttpServletRequest;
import java.util.concurrent.TimeUnit;

@Component
public class LoginUserUtil {

    @Autowired
    private RedisTemplate redisTemplate;

    @Autowired
    private HttpServletRequest httpServletRequest;

    public void setBackendUser(Object userId,Long times){
        String ipAddress = IpUtil.getIpAddress(httpServletRequest);
       setBackendUser(CommonConstant.BACKENDUSER +ipAddress,userId,times);

    }

    private void setBackendUser(String key,Object userId,Long times){
        String userIds = userId.toString();

        try {
            if (times == null || times == 0L) {
                redisTemplate.opsForValue().set(key, userIds);
            } else {
                redisTemplate.opsForValue().set(key, userIds, times, TimeUnit.MINUTES);
            }
        }catch (Exception e){
            e.printStackTrace();
        }

    }

    public Object getBackendUser(boolean istrue,String key,Long times){
        Object value = redisTemplate.opsForValue().get(CommonConstant.BACKENDUSER+key);
        if(istrue && value != null){
            setBackendUser(CommonConstant.BACKENDUSER+key,value.toString(),times);
        }
        return value;
    }

    public Object getBackendUser(){
        String ipAddress = IpUtil.getIpAddress(httpServletRequest);
        return redisTemplate.opsForValue().get(CommonConstant.BACKENDUSER+ipAddress);
    }

    private void setFrontUser(String key,Object userId,Long times){
        try {
            if(times == null || times == 0L){
                redisTemplate.opsForValue().set(key,userId.toString());
            }else{
                redisTemplate.opsForValue().set(key,userId.toString(),times, TimeUnit.MINUTES);
            }
        }catch (Exception e){
            e.printStackTrace();
        }

    }

    public void setFrontUser(Object userId,Long times){
        String ipAddress = IpUtil.getIpAddress(httpServletRequest);
        setFrontUser(CommonConstant.FRONTUSER+ipAddress,userId,times);

    }

    public Object getFrontUser(boolean istrue,String key,Long times){

        Object value = redisTemplate.opsForValue().get(CommonConstant.FRONTUSER+key);
        if(istrue && value != null){
            setFrontUser(CommonConstant.FRONTUSER+key,value.toString(),times);
        }
        return value;
    }

    public Object getFrontUser(){
        String ipAddress = IpUtil.getIpAddress(httpServletRequest);
        Object value = redisTemplate.opsForValue().get(CommonConstant.FRONTUSER+ipAddress);
        return value;
    }

    public void removeFrontUser(String key){
        try {
            String ipAddress = IpUtil.getIpAddress(httpServletRequest);
            if(key == null){
                redisTemplate.delete(CommonConstant.FRONTUSER+ipAddress);
            }else{
                redisTemplate.delete(key);
            }
        }catch (Exception e){
            e.printStackTrace();
        }
    }
    public void removeBackendUser(String key){
        try {
            String ipAddress = IpUtil.getIpAddress(httpServletRequest);

            if (key == null) {
                redisTemplate.delete(CommonConstant.BACKENDUSER + ipAddress);
            } else {
                redisTemplate.delete(key);
            }
        }catch (Exception e){
            e.printStackTrace();
        }
    }

}
